package com.jy.adscm.operation.pebase.service.impl;

import ch.qos.logback.core.joran.conditional.IfAction;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeDocument;
import com.jy.adscm.operation.pebase.bean.QueryPeDocument;
import com.jy.adscm.operation.pebase.mapper.mysql.PeDocumentMapper;
import com.jy.adscm.operation.pebase.service.PeDocumentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-04-10
 */
@Service
public class PeDocumentServiceImpl extends ServiceImpl<PeDocumentMapper, PeDocument> implements PeDocumentService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Page<PeDocument> selectPeDocument(Page<PeDocument> page, QueryPeDocument queryPeDocument) throws AdscmSystemException {
        Wrapper<PeDocument> peDocumentWrapper =new EntityWrapper<>();
        if (!AdscmBaseUtils.getInstance().isNotEmpty(queryPeDocument.getDid(),null)){
            throw  new AdscmSystemException("did不存在");
        }
        peDocumentWrapper.eq("did",queryPeDocument.getDid());
        page = super.selectPage(page, peDocumentWrapper);
        List<PeDocument> peDocuments = page.getRecords();
        for (PeDocument peDocument : peDocuments) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(peDocument.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(peDocument.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    peDocument.setCreatorName(jyUser.getName());
                }else {
                    peDocument.setCreatorName(peDocument.getCreator());
                }
            }
        }

        page.setTotal(super.selectCount(peDocumentWrapper));
        return page;
    }
}
